// Scroll Text
// author kgena_ua
// modifed AHAPXICT for DarkOne v3.1 by tedGo
// version 2.0
// WSH Panel Mod component by Tianpeng Wang

function RGB(r,g,b) { return (0xff000000|(r<<16)|(g<<8)|(b)); }
function RGBA(r, g, b, a) { return ((a << 24) | (r << 16) | (g << 8) | (b)); }
MF_SEPARATOR = 0x00000800;
MF_STRING = 0x00000000;
var font = gdi.Font("DarkOne",57,0);  //FONT
var ww,wh;

var txt_row = 4;
var s = 10, step, step1 = window.Width, step2 = window.Width;
var text1, text2, t_width, t_width1, t_width2, dir, mode, sw = 1;
var t1 = "", t2 = "", t3 = "", ti = "";
var show1, show2, show3, showi, showr;

function on_paint(gr) { 

    if (window.GetProperty("|6| Color") == 1) var font_rgb = RGB(191,228,255), block_rgb = RGB(133,159,178);   //Default   
    if (window.GetProperty("|6| Color") == 2) var font_rgb = RGB(128,192,255), block_rgb = RGB(91,136,178);   //Blue   
    if (window.GetProperty("|6| Color") == 3) var font_rgb = RGB(0,255,0), block_rgb = RGB(0,178,0);       //Green   
    if (window.GetProperty("|6| Color") == 4) var font_rgb = RGB(255,0,0), block_rgb = RGB(255,0,0);       //Red   
    if (window.GetProperty("|6| Color") == 5) var font_rgb = RGB(255,255,255), block_rgb = RGB(178,178,178);   //White   
    if (window.GetProperty("|6| Color") == 6) var font_rgb = RGB(255,255,0), block_rgb = RGB(178,178,0);     //Yellow

    show1 = window.GetProperty("|1| Title",1);
    show2 = window.GetProperty("|2| Artist",1);
    show3 = window.GetProperty("|3| Album",1);
    mode = window.GetProperty("|5| Mode",1);
    showi = window.GetProperty("|4| Info",1);
   
    if (show1 == 1) {t1 = fb.TitleFormat("$upper([%title%])").Eval();
    } else {t1 = "";}

    if (show2 == 1) {t2 = fb.TitleFormat("$upper([%artist%])").Eval();
    } else {t2 = "";}

    if (show3 == 1) {t3 = fb.TitleFormat("$upper([%album%])[ - %date%]").Eval();
    } else {t3 = "";}
   
    if (showi == 1) {ti = fb.TitleFormat("[$info(encoding)]$if(%__hdcd%, | HDCD,)[ | $info(codec)][ $info(codec_profile)][ | $info(bitrate) kbps][ | $info(samplerate) Hz][ | $info(bitspersample) bps][ | $channels()]$if($stricmp($ext(%filename_ext%),cue), | '['cue']',[ | %filesize_natural%])$if($strcmp($info(cue_embedded),yes), | '['embedded cue']',)").Eval();
    } else {ti = "";}
 
    text1 = t1 + (show1 > 0 && t2 != "" ? " - " : "") + t2 + ((show1 > 0 || show2 > 0) && t3 != "" ? " - " : "") + t3;
    text2 = showi == 1 ? ti : text1;
    text1 = showi == 1 && text1 == "" ? text2 : text1;
    t_width2 = gr.CalcTextWidth(text2, font);
    t_width1 = gr.CalcTextWidth(text1, font);
   
    if (mode == 1) {
        step = sw == 1 ? step1 : step2;
        t_width = sw == 1 ? t_width1 : t_width2;
        if (sw == 1) {step2 = ww};
        if (sw == 2) {step1 = ww};
        if (t_width < ww - s * 2) {
            if (step >= ww) {dir = "left"; step = ww };
            if (step + t_width >= ww - s) {dir = "left"};
            if (step <= 0 + s) {dir = "right"};
        } else {
            if (step + t_width <= ww - s) {dir = "right"};
            if (step >= 0 + s) {dir = "left"};
        }
    }
    if (mode == 0) { dir = "left";
        if (sw == 1) {
            if (step1 + t_width1 > ww/2) {step2 = ww};
            if (step1 + t_width1 <= 0) {step1 = ww; sw = 2};

        } else {
            if (step2 + t_width2 > ww/2) {step1 = ww};
            if (step2 + t_width2 <= 0) {step2 = ww; sw = 1};
        }
    }
 String.prototype.repeat = function(n) {
return new Array(n + 1).join(this);
}  
// ========BLOCKS======== //
    gr.GdiDrawText( "\u2598".repeat(18), font, block_rgb, 0, txt_row, ww, wh);
// ========GRADIENT======== //  
    var alpha = 235;
    if (window.GetProperty("|7| Matrix") == 7) var alpha = 235;
    if (window.GetProperty("|7| Matrix") == 8) var alpha = 255;
    gr.FillGradRect(0, -4, ww, 84, 90, RGBA(80,100,114,alpha), RGBA(19,30,38,alpha));    
// ========TEXT======== //
    d = gr.CalcTextWidth("1000", font); 
    gr.DrawString( text1, font, font_rgb, step1, txt_row, t_width1 + d, wh);
    gr.DrawString( text2, font, font_rgb, step2, txt_row, t_width2 + d, wh);
}


function on_size(){   
    ww = window.Width;   
    wh = window.Height;   
}   

var seek_timer;
var speed = window.GetProperty("|8| Speed Scroll", 50);
function on_playback_new_track(info) {
    seek_timer && window.ClearInterval(seek_timer);
    seek_timer = window.SetInterval(function() {
        if (dir == "right") {step1 = step1 + 1};
        if (dir == "right") {step2 = step2 + 1};
        if (dir == "left") {step1 = step1 - 1};
        if (dir == "left") {step2 = step2 - 1};
    window.Repaint();
    }, speed );
}

function on_playback_stop() {
    window.ClearInterval(seek_timer);
    window.Repaint();
}

function on_playback_time(){
    if(!seek_timer) on_playback_new_track();
}

function on_mouse_lbtn_up (x, y){     
    var _menu = window.CreatePopupMenu();
    var i = 1;
   
    _menu.AppendMenuItem(0x00000000, i++, "title");
    _menu.CheckMenuItem(i-1, show1);   
    _menu.AppendMenuItem(0x00000000, i++, "artist");
    _menu.CheckMenuItem(i-1, show2);   
    _menu.AppendMenuItem(0x00000000, i++, "album");
    _menu.CheckMenuItem(i-1, show3);
    _menu.AppendMenuItem(0x00000000, i++, "info");
    _menu.CheckMenuItem(i-1, showi);
    _menu.AppendMenuItem(0x00000000, i++, "scrolling mode: " + (mode == 0 ? "L-R" : "L"));

    ret = _menu.TrackPopupMenu(x, y);
    if (ret == 0) return;

    switch (ret) {
    case 1:
        window.SetProperty("|1| Title", show1 == 0 ? 1 : 0);
        break;
    case 2:
        window.SetProperty("|2| Artist", show2 == 0 ? 1 : 0);
        break;
    case 3:
        window.SetProperty("|3| Album", show3 == 0 ? 1 : 0);
        break;
    case 4:
        window.SetProperty("|4| Info", showi == 0 ? 1 : 0);
        break;
    case 5:
        window.SetProperty("|5| Mode", mode == 0 ? 1 : 0);
        break;
    }
    _menu.Dispose();
    return true;
}
// ========Menu======== //
function on_mouse_rbtn_up(x, y) {

    var _menu = window.CreatePopupMenu();
   
    _menu.AppendMenuItem(MF_STRING, 1, "Default");
    _menu.AppendMenuItem(MF_STRING, 2, "Blue");
    _menu.AppendMenuItem(MF_STRING, 3, "Green");
    _menu.AppendMenuItem(MF_STRING, 4, "Red");
    _menu.AppendMenuItem(MF_STRING, 5, "White");
    _menu.AppendMenuItem(MF_STRING, 6, "Yellow")
    _menu.CheckMenuRadioItem(1, 6, window.GetProperty("|6| Color", 1));   
    _menu.AppendMenuItem(MF_SEPARATOR, 0, 0)
    _menu.AppendMenuItem(MF_STRING, 7, "Matrix ON")
    _menu.AppendMenuItem(MF_STRING, 8, "Matrix OFF")
    _menu.CheckMenuRadioItem(7, 8, window.GetProperty("|7| Matrix", 7));
    _menu.AppendMenuItem(MF_SEPARATOR, 0, 0)
    _menu.AppendMenuItem(MF_STRING, 9, "Properties...");
    _menu.AppendMenuItem(MF_STRING, 10, "Configure...");

    idx = _menu.TrackPopupMenu(x, y);
    switch (idx) {

    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
        window.SetProperty("|6| Color", idx);   
        window.Repaint();   
        break;   
    case 7:
    case 8:   
        window.SetProperty("|7| Matrix", idx);   
        window.Repaint();   
        break;     
    
    case 9:
        window.ShowProperties();
        break;
    case 10:
        window.ShowConfigure();
        break;
}
    _menu.Dispose();
return true
}